package cn.bjit.controller;


import cn.bjit.request.LockProductRequest;
import cn.bjit.service.ProductService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.ProductVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/api/product/v1")
public class ProductController {
    @Resource
    private ProductService productService;

    @ApiOperation("分页查询商品列表")
    @GetMapping("page")
    public JsonData pageProductList(
            @ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam("每页显示多少条") @RequestParam(value = "size", defaultValue = "5") int size
    ) {
        Map<String, Object> pageResult = productService.page(page, size);
        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("根据id查看详细信息")
    @GetMapping("detail/{product_id}")
    public JsonData detail(@ApiParam(value = "商品id",required = true) @PathVariable("product_id") long productId) {
        ProductVO productVO = productService.findDetailById(productId);
        return JsonData.buildSuccess(productVO);
    }

    @ApiOperation("商品库存锁定")
    @GetMapping("lock_products")
    public JsonData lockProducts(@ApiParam("商品库存锁定")@RequestBody LockProductRequest lockProductRequest) {
        JsonData jsonData= productService.lockProductStock(lockProductRequest);
        return jsonData;
    }

}

